package flashcardtest;

import java.io.IOException;
import java.util.Map;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;

public class EditPhotoServlet extends HttpServlet implements Servlet {

	private static final long serialVersionUID = 1L;
	BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
	ImagesService imageService = ImagesServiceFactory.getImagesService();

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// read data from form
		String title = request.getParameter("title");
		String date = request.getParameter("date");
		BlobKey blobKey = (BlobKey) request.getSession().getAttribute("blobKey");
		
		// register photo in datastore with reference to blobkey
		
		if (blobKey != null) {
			//response.sendRedirect("/serve?blob-key=" + blobKey.getKeyString());
			DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
			Entity item = new Entity("Photo");
			item.setProperty("title", title);
			item.setProperty("date", date);
			item.setProperty("blobKey", blobKey);
			datastore.put(item);
		}
		
		
		// redirect to album view
		
		//String type = request.getClass().getName();
		//request.getSession().setAttribute("message", "type is " + type);
		//request.getSession().setAttribute("message", "size of map: " + Integer.toString(blobs.size()));

		//request = new HttpServletRequesthttp();
		//getServletContext().getRequestDispatcher("/album.jsp").forward(request, response);
		response.sendRedirect("/album.jsp");


	
	}

}
